package com.atguigu.jxc.service.impl;

import com.alibaba.fastjson.JSON;
import com.atguigu.jxc.dao.CustomerReturnListDao;
import com.atguigu.jxc.dao.CustomerReturnListGoodsDao;
import com.atguigu.jxc.dao.SaleListDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.CustomerReturnList;
import com.atguigu.jxc.entity.CustomerReturnListGoods;
import com.atguigu.jxc.entity.vo.SaleCountVo;
import com.atguigu.jxc.service.CustomerReturnListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 杨林
 * @create 2023-01-29 23:30 星期日
 * description:
 */
@Service
public class CustomerReturnListServiceImpl implements CustomerReturnListService {

    @Autowired
    CustomerReturnListDao customerReturnListDao;

    @Autowired
    CustomerReturnListGoodsDao customerReturnListGoodsDao;

    /**
     * 客户退货单查询（可查询条件：退货单号、客户、退款状态）
     * @param returnNumber
     * @param customerId
     * @param state
     * @param sTime
     * @param eTime
     * @return
     */
    @Override
    public Map<String, Object> list(String returnNumber,
                                    Integer customerId,
                                    Integer state,
                                    String sTime,
                                    String eTime) {

        Map<String, Object> map = new HashMap<>();

        List<CustomerReturnList> customerReturnLists = customerReturnListDao.list(returnNumber,customerId,
                                                                                    state,sTime,eTime);
        map.put("rows",customerReturnLists);
        return map;
    }

    /**
     * 删除退货单
     * @param customerReturnListId
     * @return
     */
    @Override
    public ServiceVO delete(Integer customerReturnListId) {

        //根据customerReturnListId获取退货单下的商品信息
        List<CustomerReturnListGoods> list = new ArrayList<>();
        list = customerReturnListGoodsDao.ifHasCustomerReturnListGoods(customerReturnListId);
        if (list.size() > 0){
            return new ServiceVO(ErrorCode.GOODS_TYPE_ERROR_CODE,ErrorCode.GOODS_TYPE_ERROR_MESS);
        }
        customerReturnListDao.delete(customerReturnListId);
        return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }


    /**
     * 客户退货统计（可根据 商品类别、商品编码或名称 条件查询）
     * @param sTime
     * @param eTime
     * @param goodsTypeId
     * @param codeOrName
     * @return
     */
    @Override
    public String count(String sTime, String eTime, Integer goodsTypeId, String codeOrName) {

        List<SaleCountVo> saleCountVoList =
                customerReturnListDao.count(sTime,eTime,goodsTypeId,codeOrName);
        return JSON.toJSONString(saleCountVoList);
    }
}
